package com.android.support.lib.universal.view.general;


import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageButton;

import com.android.support.lib.universal.general.Session;
import com.android.support.lib.universal.util.UtilSharedPreferences;

/**
 * 根据自身ID保存自身的位置坐标。能够记录自己移动后的坐标位置。
 * Created by 彭治铭 on 2016/5/12.
 */
public class LayoutImageButton extends ImageButton {
    //根据控件ID保存位置信息
    UtilSharedPreferences sharedPreferences = UtilSharedPreferences.getSharedPreferencesUtil(getId() + "");
    private int mDefault = 1000 * 1000;
    private int mLeft, mRight, mTop, mBottom;

    public LayoutImageButton(Context context) {
        super(context);
    }

    public LayoutImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //是否为用户手动操控
    private Boolean IsUser = false;

    @Override
    public void layout(int l, int t, int r, int b) {
        if (IsUser || getmLeft() == mDefault || getmTop() >= getmBottom() || getmLeft() >= getmRight()) {
//            Log.e("test","l: "+l+"r: "+r+"t: "+t+"b: "+b);
            if (l < r && t < b) {
                super.layout(l, t, r, b);
                this.mLeft = l;
                this.mRight = r;
                this.mTop = t;
                this.mBottom = b;
            } else {
                super.layout(mLeft, mTop, mRight, mBottom);
            }

        } else {
//            Log.e("test","left: "+getmLeft()+"right: "+getmRight()+"TOP: "+getmTop()+"bottom: "+getmBottom());
            super.layout(getmLeft(), getmTop(), getmRight(), getmBottom());
        }
    }

    //记录坐标位置
    public void setLayout() {
        Session.getSession().put("mLeft", getLeft());
        Session.getSession().put("mTop", getTop());
        Session.getSession().put("mRight", getRight());
        Session.getSession().put("mBottom", getBottom());
    }

    //保持坐标位置（控件消亡是会自动调用）
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        sharedPreferences.setInt("mLeft", getLeft());
        sharedPreferences.setInt("mTop", getTop());
        sharedPreferences.setInt("mRight", getRight());
        sharedPreferences.setInt("mBottom", getBottom());
    }

    public int getmLeft() {
        if (Session.getSession().get("mLeft") != null) {
            return (int) Session.getSession().get("mLeft");
        }
        return sharedPreferences.getInt("mLeft", mDefault);
    }

    public int getmTop() {
        if (Session.getSession().get("mTop") != null) {
            return (int) Session.getSession().get("mTop");
        }
        return sharedPreferences.getInt("mTop", mDefault);
    }

    public int getmRight() {
        if (Session.getSession().get("mRight") != null) {
            return (int) Session.getSession().get("mRight");
        }
        return sharedPreferences.getInt("mRight", mDefault);
    }

    public int getmBottom() {
        if (Session.getSession().get("mBottom") != null) {
            return (int) Session.getSession().get("mBottom");
        }
        return sharedPreferences.getInt("mBottom", mDefault);
    }

    public void setUser(Boolean user) {
        IsUser = user;
    }

    public LayoutImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}
